import { ConnectorDataSource } from "@/services/connector";
import { dsFindOne } from "../datasource";
import { SCHEDULER_TASK_CLASS_IDENTIFIER, SCHEDULER_TASK_TEMPLATE_CLASS_IDENTIFIER, WORKFLOW_CLASS_IDENTIFIER } from "@/services/constants";
import { ComparsionOperator } from "../types";

export interface Task {
    id?: number;
    identifier: string;
    title: string;
    template_identifier: string;
    start_time: string;
    end_time: string;
    cron: string;
    times: string;
    try_times: string;
    timeout: string;
    enable: boolean;
    variables?: string;
    description?: string;
}

export const schedulerInsertTask = () => {

}

export const exportTasks = async (tasks: string[], filesPath: string, sqlite: ConnectorDataSource) => {
    //查询所有的任务
    for (let i = 0; i < tasks.length; i++) {
        let taskIndetifier = tasks[i];
        //
        let item = await dsFindOne(0, SCHEDULER_TASK_CLASS_IDENTIFIER, {
            attributes: [{
                name: 'identifier',
                operator: ComparsionOperator.Equal,
                value: taskIndetifier,
            }]
        }, {
            class_selector: {
                "template": [
                    SCHEDULER_TASK_TEMPLATE_CLASS_IDENTIFIER,
                    {
                        relations: [
                            {
                                conditions: {
                                    related_at: 'template_identifier'
                                }
                            }
                        ]
                    },
                    {
                        class_selector: {
                            "workflow": [
                                WORKFLOW_CLASS_IDENTIFIER,
                                {
                                    relations: [
                                        {
                                            conditions: {
                                                related_at: 'content'
                                            }
                                        }
                                    ]
                                },
                                {
                                    class_selector: {}
                                }
                            ]
                        }
                    }
                ]
            }
        });
        //
        console.log('====', item);
    }
}

export default {}